package cn.wolfcode.crm.util;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class HandlerExceptionUtil {
    @ExceptionHandler(UnauthorizedException.class)
    public void handlerException(HttpServletResponse response, HandlerMethod handlerMethod) throws IOException {
        ResponseBody responseBody = handlerMethod.getMethodAnnotation(ResponseBody.class);
        if (responseBody != null) {
            JsonResult jsonRest = new JsonResult();
            response.getWriter().print(new ObjectMapper().writeValueAsString(new JsonResult().mark("你没有此权限")));
        }else{
            response.sendRedirect("/static/common/nopermission.jsp");
        }

    }
}
